"""
用户相关的API端点

处理用户信息相关的请求，包括个人信息管理、密码修改等。
"""

from typing import Any

from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession

from app.db.session import get_db
from app.models.user import User
from app.modules.auth.schemas import UserProfile, UserUpdate, PasswordChange
from app.modules.auth.services import auth_service
from app.modules.auth.deps import get_current_user


router = APIRouter()


@router.get("/me", response_model=UserProfile)
async def get_user_me(current_user: User = Depends(get_current_user)):
    """
    获取当前用户信息
    
    返回当前登录用户的个人资料。
    """
    return current_user


@router.put("/me", response_model=UserProfile)
async def update_user_me(
    user_data: UserUpdate,
    db: AsyncSession = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """
    更新当前用户信息
    
    更新当前登录用户的个人资料。
    """
    updated_user, error = await auth_service.update_user_profile(
        db, user_id=current_user.id, full_name=user_data.full_name
    )
    
    if error:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=error
        )
    
    return updated_user


@router.post("/me/change-password", status_code=status.HTTP_200_OK)
async def change_password(
    password_data: PasswordChange,
    db: AsyncSession = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """
    修改当前用户密码
    
    更新当前登录用户的密码。
    """
    success, error = await auth_service.change_password(
        db, 
        user_id=current_user.id, 
        current_password=password_data.current_password, 
        new_password=password_data.new_password
    )
    
    if not success:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=error or "修改密码失败"
        )
    
    return {"message": "密码已成功修改"} 